<?php
/**
 * 重写 CDbCommand，添加日志操作
 */
class ADbCommand extends CDbCommand
{
    /**
     * @override 增加日志操作
     */
    public function execute($params=array())
    {
        $result = parent::execute($params);
        //如果sql异常则不记录
        try {
            if($result)
            {
                $transaction = $this->getConnection()->getCurrentTransaction();
                $transaction = empty($transaction) ? '' : 'transaction ';
                $sql = $this->getText();
                $sql = trim(strtolower($sql));
                $message = '';
                if(strpos($sql,'delete') === 0)
                {
                    $message = 'delete';
                }
                if(strpos($sql,'update') === 0)
                {
                    $message = 'update';
                }
                if(strpos($sql,'insert') === 0)
                {
                    $message = 'add';
                }
                if(!empty($message))
                {
                    //add Yii::app()->user->id;
                    Yii::log('sql is ' . $transaction . $message . json_encode(array('sql' => $sql, 'params' => $params)) , 'error', 'api.log');
                }

            }
        } catch (Exception $e) {
            Yii::log('api log error ' . $e->getMessage(), 'error', 'api.log');
        }
        return $result;
    }
}
